package com.wxapp.dispatch.service.job.http;

import com.wxapp.common.base.util.jackson.JacksonUtil;
import com.wxapp.common.constant.pojo.vo.HttpRequestVo;
import com.wxapp.common.constant.pojo.vm.ResultVm;
import com.wxapp.common.util.http.HttpRequestUtil;
import com.wxapp.dispatch.api.pojo.vo.dispatch.QDispatchVo;
import com.wxapp.dispatch.service.job.BaseJob;
import org.quartz.JobExecutionContext;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author lgl
 */
@Component(value = "HttpJob")
public class HttpRequestJob extends BaseJob {

    @Override
    protected ResultVm<Object> toDealJob(JobExecutionContext context, QDispatchVo dispatchVm) {
        Map<String, Object> data = dispatchVm.getParam() == null ? null : JacksonUtil.parseObject(dispatchVm.getParam(), Map.class);
        var request = new HttpRequestVo<ResultVm>(dispatchVm.getUrl(), null, data,ResultVm.class,dispatchVm.getMapHeader());
        //请求
        switch (dispatchVm.getRequestType().getValue()) {
            case "POST":
                request.setMethod(HttpMethod.POST);
                break;
            case "DELETE":
                request.setMethod(HttpMethod.DELETE);
                break;
            case "PUT":
                request.setMethod(HttpMethod.PUT);
                break;
            case "GET":
            default:
                request.setMethod(HttpMethod.GET);
                break;
        }
        return HttpRequestUtil.request(request);
    }
}
